﻿@using nwrole.toolbox.sync.entity
@model List<nwrole.toolbox.sync.entity.SyncEntity>

@{
    ViewBag.Title = "Index";
}

<div>
   
    <table class="box">
        <tr>
            <th>Name</th>
            <th>Table</th>
            <th>Last sync</th>
            <th>Last import</th>
            <th>Deployed destination</th>
            <th>Sync type</th>
            <th>Enabled</th>
            <th><a href="" onclick="return SyncAllSyncEntities();">Sync All</a></th>
        </tr>
		@{ bool even = true; }
        @foreach (var e in Model.OrderBy(x => x.SortIndex))
        {
            <tr @((even = !even) ? "class=even" : "")>
                <td class="form">
                    <a href="@Url.Action("Show", new {id=e.Id})">:@e.Name</a>
                </td>        
                <td class="form">
                    @e.SourceTableName
                </td>        
                <td class="form">
                    @e.SyncDat
                </td>        
                <td class="form">
                    @e.ImportDat
                </td>        
                <td class="form">
                    @e.DeployDestinationDat
                </td>        
                <td class="form">
                    @if(e.SyncMode != SyncModeEnum.FileSync)
                    {
                        <a href="" onclick="return ToggleSyncMode(this, @e.Id);">@e.SyncMode</a>
                    } else
                    {
                        @e.SyncMode
                    }
                </td>
                <td class="form">
                    <a href="" onclick="return ToggleSyncEnabled(this, @e.Id);">@e.SyncEnabledString</a>
                </td>    
                <td class="form">
                    @if(e.SyncMode != SyncModeEnum.FileSync)
                    {
                        <text>
                        <a href="" onclick="return DeleteSyncEntity(this, @e.Id);">Delete</a> |
                        <a href="" onclick="return DeploySource(this, @e.Id);">DeploySource</a> |
                        <a href="" onclick="return DeployDestination(this, @e.Id);">DeployDestination</a> |
                        </text>
                    }
                    <a href="" onclick="return SyncSyncEntity(this, @e.Id);">Sync</a>
                </td>  
            </tr>
        }
    </table>    
</div>
   
<script type="text/javascript">
    function ToggleSyncEnabled(obj, colId) {
        $.get('@Url.Action("ToggleSyncEnabled")', { id: colId, x: Math.random() }, function (result) {
            if (result.success) {
                if (result.newValue) {
                    $(obj).text('True');
                } else {
                    $(obj).text('False');
                }
            }
            else {
                alert(result.success);
            }
        });

        return false;
    }

    function ToggleSyncMode(obj, colId) {
        $.get('@Url.Action("ToggleSyncMode")', { id: colId, x: Math.random() }, function (result) {
            if (result.success) {
                $(obj).text(result.newValue);
            }
            else {
                alert(result.success);
            }
        });

        return false;
    }

    function ImportSchema() {
        $.get('@Url.Action("ImportSchema")', { a: 1, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function DeleteSyncEntity(obj, id) {
        $.get('@Url.Action("DeleteSyncEntity")', { id: id, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function DeploySource(obj, id) {
        $.get('@Url.Action("DeploySource")', { id: id, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function DeployDestination(obj, id) {
        $.get('@Url.Action("DeployDestination")', { id: id, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function SyncAllSyncEntities() {
        $.get('@Url.Action("SyncAllSyncEntities")', { id: @ViewBag.InstanceId, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }
    
    function SyncSyncEntity(obj, id) {
        $.get('@Url.Action("SyncSyncEntity")', { id: id, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

</script>